package cn.hd.controller;

import cn.hd.entity.*;
import cn.hd.service.ProductService;
import cn.hd.service.ProductdetailService;
import cn.hd.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@CrossOrigin
@Component
public class ProductdetailController {
    @Autowired
    private ProductdetailService Productdetailservice;

    @Autowired
    private ProductService productService;

    @Scheduled(cron = "0 0 1 * * ? ")
    // 每晚凌晨1点执行一次
    public void taskCycle() {
        int pageNum = 1;
        int pageSize = 8;
        String pname = "";
        this.selectProductdetail(pageNum,pageSize, pname);
    }
    @Autowired
    private UserService userService;

    @RequestMapping("/selectProductdetail")
    public Object selectProductdetail(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "8") int pageSize, String pname){
        Map map = new HashMap<>();
        map.put("pname",pname);
        PageInfo<Productdetail> pro = Productdetailservice.selectProductdetail(pageNum, pageSize, map);
        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }
    //通过审核
    @RequestMapping("/updateStatuSuccess")
    public Object updateStatuSuccess(long pid){
        int i = Productdetailservice.updateStatuSuccess(pid);
        if (i!=0){
            return new Result("200","审核通过！",i);
        }
        return new Result("400","审核通过失败！",null);
    }


    //审核状态
    @RequestMapping("/updateStatuFailed")
    public Object updateStatuFailed(long pid){
        int i = Productdetailservice.updateStatuFailed(pid);
        if (i!=0){
            return new Result("200","审核不通过成功！",i);
        }
        return new Result("400","审核不通过失败！",null);
    }

    @RequestMapping("/selectByProShenhe")
    public Object selectByProShenhe(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "3") int pageSize, long uid){
        PageInfo<Productdetail> pro = Productdetailservice.selectByProShenhe(pageNum,pageSize,uid);
        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }

    @RequestMapping("/updateProState")
    public Object updateProState(long pid){
        int i = Productdetailservice.updateProState(pid);
        if (i!=0){
            return new Result("200","申请成功！",i);
        }
        return new Result("400","申请失败！",null);
    }
    //逻辑删除中间表和项目表
    @RequestMapping("/delProStatus")
    public Object delProStatus(long pid){
        int i = Productdetailservice.delProStatus(pid);
        int j = Productdetailservice.deluserPocduct(pid);
        if (i!=0&&j!=0){
            return new Result("200","删除成功！",i);
        }
        return new Result("400","删除失败+！",null);
    }

    @RequestMapping("/selectProductdetailByState")
    public Object selectProductdetailByState(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "4") int pageSize, String pname){
        Map map = new HashMap<>();
        map.put("pname",pname);
        PageInfo<Productdetail> pro = Productdetailservice.selectProductdetailByState(pageNum,pageSize,map);
        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }
    //成功的项目
    @RequestMapping("/selectProductdetailSuccess")
    public Object selectProductdetailSuccess(int pageNum,int pageSize){
        PageInfo<Productdetail> pro = Productdetailservice.selectProductdetailSuccess(pageNum,pageSize);

        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }
    //进行中的项目
    @RequestMapping("/selectProductdetailIng")
    public Object selectProductdetailIng(int pageNum,int pageSize){
        PageInfo<Productdetail> pro = Productdetailservice.selectProductdetailIng(pageNum,pageSize);
        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }


    //失败的项目
    @RequestMapping("/selectProductdetailFailed")
    public Object selectProductdetailFailed(int pageNum,int pageSize){
        PageInfo<Productdetail> pro = Productdetailservice.selectProductdetailFailed(pageNum,pageSize);
        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }
    //查询众筹钱最多
    @RequestMapping("/selectByMoney")
    public Object selectByMoney(int pageNum,int pageSize,int state){
        PageInfo<Productdetail> pro = Productdetailservice.selectByMoney(pageNum,pageSize,state);
        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }
    //查询众筹人数最多
    @RequestMapping("/selectByPerson")
    public Object selectByPerson(int pageNum,int pageSize,int state){
        PageInfo<Productdetail> pro = Productdetailservice.selectByPerson(pageNum,pageSize,state);
        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }
    //    最近发布
    @RequestMapping("/selectDateNow")
    public Object selectDateNow(int pageNum,int pageSize,int state){
        PageInfo<Productdetail> pro = Productdetailservice.selectDateNow(pageNum,pageSize,state);
        if (pro.getList().size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }

    //根据一级name查询二级分类
    @RequestMapping("/selectProductOneBytwo")
    public Object selectProductOneBytwo(String one){
        List<ProductLevel> pro = Productdetailservice.selectProductOneBytwo(one);
        if (pro.size()!=0){
            return new Result("200","查询成功！",pro);
        }
        return new Result("400","查询失败！",null);
    }





    @RequestMapping("/uploadImg")
    public Object uploadImg(MultipartFile file){
        String fname = file.getOriginalFilename();
        String  id = "";
        if (fname.contains("/")){
            fname = fname.substring(fname.lastIndexOf("/")+1);
        }
        String[] split = UUID.randomUUID().toString().split("-");
        for (String s:split){
            id += s;
        }
        fname =id +"-"+fname;
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        String format = sf.format(date);
        File f = new File("D:\\imgs",format);
        if (!f.exists()){
            f.mkdirs();
        }
        try {
            file.transferTo(new File(f,fname));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result("200",fname,null);
    }
    @RequestMapping("/showImgx")
    public void yulan(String fname, HttpServletResponse response) throws IOException {
        File file = new File("d:/imgs/2021-12/",fname);
        FileInputStream fin = new FileInputStream(file);
        //预览
        response.setHeader("Content-Disposition", "filename="+fname);//预览
        ServletOutputStream fout = response.getOutputStream();
        IOUtils.copy(fin,fout);
        fout.close();
        fin.close();
    }



    //zjc开始-----
    @RequestMapping("/selectFQPro")
    public Object selectFQPro(@RequestParam(defaultValue = "1")Integer pagenum, Integer pagesize,Integer uid,Integer state){
        PageInfo<Productdetail> page = Productdetailservice.selectFQPro(pagenum,pagesize,uid,state);
        if (page.getList().size() != 0){
            return new Result("200","查询成功",page);
        }
        return new Result("400","查询失败",null);
    }

    @RequestMapping("/selectProByUid")
    public Object selectProByUid(@RequestParam(defaultValue = "1")int pagenum, int pagesize,int uid){
        PageInfo<Productdetail> page = Productdetailservice.selectProByUid(pagenum, pagesize, uid);
        if (page.getList().size() != 0){
            return new Result("200","查询成功",page);
        }
        return new Result("400","查询失败",null);
    }

    @RequestMapping("/quxiaoguanzhu")
    public Object quxiaoguanzhu(Integer uid,Integer pid){
        int i = Productdetailservice.quxiaoguanzhu(uid,pid);
        if (i != 0){
            return new Result("200","取消成功",null);
        }
        return new Result("400","取消失败",null);
    }

    @RequestMapping("/selectProductDetailById")
    public Object selectProductDetailById(long id){
        Productdetail pro = Productdetailservice.selectProductDetailById(id);
        if (pro != null){
            return new Result("200","查询成功",pro);
        }
        return new Result("400","查询失败",null);
    }


    @RequestMapping("/selectByPid")
    public Object selectByPid(long pid){
        Productdetail productdetail = Productdetailservice.selectByPid(pid);
        if (productdetail!=null){
            return new Result("200","查询成功！",productdetail);
        }else {
            return new Result("400","查询失败！",null);
        }
    }
    @RequestMapping("/addPros")
    public Object addPros(Productdetail productdetail, Integer uid, String dd, Product product){
//        product.setName("admin");
        User user = userService.selectUserById(uid);
        product.setName(user.getUsername());
        if (dd.contains(",")){
            String s = dd.substring(dd.lastIndexOf(",")+1); //二级分类
            int pow1 = Integer.parseInt(s);
            productdetail.setPower(pow1);
            product.setSecondclass(pow1);
            String s1=dd.substring(0,1); //一级分类
            int pow=Integer.parseInt(s1);
            product.setFirstclass(pow);
        }
//        int pow2 = Integer.parseInt(dd);
//        productdetail.setPower(pow2);
//
//        product.setName("admin");
//        if (dd.contains(",")){
//            String s = dd.substring(dd.lastIndexOf(",")+1);
//            int pow22 = Integer.parseInt(s);
//            product.setSecondclass(pow22);
//            String s1=dd.substring(0,1);
//            int pow1=Integer.parseInt(s1);
//            product.setFirstclass(pow1);
//        }
        else{
            int pow = Integer.parseInt(dd);
            productdetail.setPower(pow);
            product.setFirstclass(pow);
            product.setSecondclass(0);
        }

        int i = Productdetailservice.addProduct(productdetail);
        int i1 = Productdetailservice.addFQpro(uid,productdetail.getPid());
        int i2 = productService.addProduct(product);
        if (i != 0 && i1!=0 && i2!=0){
            return new Result("200","添加成功！",productdetail);
        }
        return new Result("400","添加失败！",null);
    }


    @RequestMapping("/Intx")
    public Object Intx(){
        List<Integer> a = new ArrayList<>();
        for (int i = 0;i < 5;i++){
            a.add(i);
        }
        return new Result("200","完成",a);
    }

    @RequestMapping("/selectFLzbaa")
    public Object selectFLzbaa(){
        List<Integer> integers = Productdetailservice.selectFLzbaa();
        return new Result("200","完成",integers);
    }
    @RequestMapping("/selectFLmc")
    public Object selectFLmc(){
        List<String> mc = Productdetailservice.selectFLmc();
        return new Result("200","完成",mc);
    }

    @RequestMapping("/selectYE")
    public Object selectYE(){
        List<ProductLevel> productLevels = Productdetailservice.selectYJ();
        List<Integer> a = new ArrayList<>();
         for (int i =0;i < productLevels.size();i++ ){
          a.add(productLevels.get(i).getCount());
         }
        List<String> b = new ArrayList<>();
        for (int i =0;i < productLevels.size();i++ ){
            b.add(productLevels.get(i).getName());
        }
        Map map = new HashMap();
        map.put("count",a);
        map.put("name",b);
        return new Result("200","完成",map);
    }


    @RequestMapping("/selectByp")
    public Object selectByp(Long pid){
        Productdetail productdetail = Productdetailservice.selectByp(pid);
        if (productdetail!=null){
            return new Result("200","查询成功！",productdetail);
        }else {
            return new Result("400","查询失败！",null);
        }
    }
    @RequestMapping("/deleteProxqbyPid")
    public Object deleteProxqbyPid(Long pid,Long uid){
        System.out.println(pid+""+uid);
        int i = Productdetailservice.deleteProxqbyPid(pid);
        if (i != 0){
            Productdetailservice.deleteProUserbyPid(uid,pid);
            return new Result("200","删除成功",null);
        }
        return new Result("400","正在众筹，无法删除",null);
    }
    @RequestMapping("/addguanzhu")
    public Object addguanzhu(String uid,long pid){
        int id = Integer.parseInt(uid);
        int i = Productdetailservice.addguanzhu(id,pid);
        if (i != 0){
            return new Result("200","收藏成功",i);
        }
        return new Result("400","收藏失败",null);
    }

}

